package com.example.user.myapply

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.example.base.entity.ApplyEntity
import com.example.base.entity.ParkEntity
import com.example.base.entity.RepairEntity
import com.example.base.entity.VisitEntity
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.HttpType
import com.example.base.mvi.UiState
import com.example.user.R
import com.example.user.databinding.ActivityMyApplyBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/user/MyApplyActivity")
@AndroidEntryPoint
class MyApplyActivity : BaseActivity<ActivityMyApplyBinding,MyApplyIntent,MyApplyViewModel>() {
    val applyAdapter by lazy { ApplyAdapter() }
    var list = mutableListOf<ApplyEntity>()
    var type = ""
    override fun getLayout(): Int = R.layout.activity_my_apply

    override fun getVM(): Class<MyApplyViewModel>  = MyApplyViewModel::class.java

    override fun initView() {
        binding.rvApply.apply {
            adapter = applyAdapter
            layoutManager = LinearLayoutManager(this@MyApplyActivity)
        }
        //刚进入页面，没有选中任意一个转态
        viewModel.sendIntent(MyApplyIntent.getPark("标准车位"))
        viewModel.sendIntent(MyApplyIntent.getRepair)
        viewModel.sendIntent(MyApplyIntent.getVisit)
        //点击不同的申请数据，进行展示
        binding.rg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb_park->type = "车位申请"
                R.id.rb_repair->type = "维修申请"
                R.id.rb_visit->type = "预约申请"
            }
            applyAdapter.apply {
                data.clear()
                val entities = list.filter { it.applyName == type }
                addData(entities)
                notifyDataSetChanged()
            }
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.uiState.collect{
                when(it){

                    is UiState.onSuccess<*> -> {
                        when(it.type){
                            HttpType.DEFAULT -> {
                                val parkEntity = it.data as ParkEntity
                                parkEntity.filter { it.userId==SPUtils.getInstance().getInt("uid") }.forEach {
                                    list.add(ApplyEntity("车位申请","${it.areaName}${it.spaceNumber}车牌号${it.licensePlate}",it.spaceStatus,""))
                                }

                            }
                            HttpType.REPAIR -> {
                                val repairEntity = it.data as RepairEntity
                                repairEntity.filter { it.userId==SPUtils.getInstance().getInt("uid") }.forEach {
                                    list.add(ApplyEntity("维修申请",it.faultDescription,it.processingStatus,it.repairTime))
                                }
                            }
                            HttpType.VISIT -> {
                                val visitEntity = it.data as VisitEntity
                                visitEntity.filter { it.employeeId==SPUtils.getInstance().getInt("uid") }.forEach {
                                    list.add(ApplyEntity("预约申请","${it.visitorName}(1人)${it.purpose}",it.visitStatus,it.appointmentTime))
                                }
                            }
                            else->{}
                        }
                        applyAdapter.apply {
                            data.clear()
                            addData(list)
                            notifyDataSetChanged()
                        }
                    }
                    else->{}
                }
            }
        }
    }

}